﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PlainOldVersionControl.Domain;
using PlainOldVersionControl.Data;
using System.Data;

namespace PlainOldVersionControl.BusinessLogic.Base
{
    public class CommitBL : BaseBL
    {
         #region Constructor

        public CommitBL(Commit c) : base(c) { }

        #endregion

        #region Properties

        internal new Commit DomainObject
        {
            get { return base.DomainObject as Commit; }
        }

        #endregion

        #region Public Methods

        public override void Validate()
        {
            // TODO : put in validation
        }

        #endregion

        #region Public Static Methods

         public static void GetAllCommits(System.Data.DataTable tb)
        {
            using (Tx.Start())
            {
                tb.Columns.Add(ColumnNames.Commit, typeof(string));
                tb.Columns.Add(ColumnNames.Date, typeof(DateTime));
                tb.Columns.Add(ColumnNames.Author, typeof(string));
                tb.Columns.Add(ColumnNames.Tree, typeof(string));

                foreach (Commit c in GetAll<Commit>().OrderByDescending(x => x.CreatedDate))
                {
                    DataRow dr = tb.NewRow();
                    dr[ColumnNames.Commit] = c.Hash;
                    dr[ColumnNames.Date] = c.CreatedDate;
                    dr[ColumnNames.Author] = c.Author;
                    dr[ColumnNames.Tree] = c.Tree.Hash;
                    tb.Rows.Add(dr);
                }
            }
        }

         public static void ViewCommit(string CHash, IViews.Base.ICommit view)
         {
             using (Tx.Start())
             {
                 Commit c = GetById<Commit>(CHash);

                 if (c == null)
                     throw new Exception("Invalid Commit Hash");

                 view.Author = c.Author;
                 view.CreatedDate = c.CreatedDate;
                 view.Hash = c.Hash;
                 view.Message = c.Message;                 
             }
         }

        #endregion

        #region Private Methods

        #endregion

         public class ColumnNames
         {
             public static string Commit { get { return "File"; } }
             public static string Date { get { return "Date"; } }
             public static string Author { get { return "Author"; } }
             public static string Tree { get { return "Tree"; } }
         }         
    }
}
